//****************************************************
// Porman Project 2
// Filename: CHRTimer.cpp
// Date: 2010/08/07
// Author: KengYu Lin
// Modify:
//****************************************************
#include <windows.h>
#include <assert.h>
#include "CHRTimer.h"

CHRTimer::CHRTimer(void) : 
m_start(),
m_stop(),
m_dfrequency()
{
    m_dfrequency = this->getFrequency();
}

double CHRTimer::getFrequency(void)
{
    LARGE_INTEGER proc_freq;

    if(!::QueryPerformanceFrequency(&proc_freq)) 
    {
        assert(0);
        return 1;
    }

    return proc_freq.QuadPart;
}

void CHRTimer::startTimer(void)
{
    DWORD_PTR oldmask = ::SetThreadAffinityMask(::GetCurrentThread(), 0);

    ::QueryPerformanceCounter(&m_start);

    ::SetThreadAffinityMask(::GetCurrentThread(), oldmask);
}

double CHRTimer::stopTimer(void)
{
    DWORD_PTR oldmask = ::SetThreadAffinityMask(::GetCurrentThread(), 0);

    ::QueryPerformanceCounter(&m_stop);
    ::SetThreadAffinityMask(::GetCurrentThread(), oldmask);

    return ((m_stop.QuadPart - m_start.QuadPart) / m_dfrequency);
} 